<template>
  <div class="item1">
    <div class="breadcrumb" :gutter="24">
      {{ $t("basic.measurement_unit") }}

    </div>
    <el-form ref="form" :model="formData" label-width="90px" size="small">
      <el-row :gutter="28">
        <el-col :xs="3" :sm="1" :md="1" :lg="1" :xl="1">
          <el-form-item label-width="0px">
            <el-button type="primary" size="small" :icon="'iconfont ' + icon" class="change" @click="changePattern"></el-button>
          </el-form-item>
        </el-col>
        <el-col :xs="12" :sm="8" :md="8" :lg="6" :xl="4">
          <el-form-item :label="$t('common.keyword')">
            <el-input v-model="formData.keyword" clearable placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="3" :sm="2" :md="2" :lg="2" :xl="2">
          <el-form-item label-width="0px">
            <el-button type="primary" size="small" @click="handleSearch">{{ $t("common.query") }}</el-button>
          </el-form-item>
        </el-col>
        <div class="btngroup">
          <el-button type="primary" size="small" @click="handleAdd()">{{ $t("common.add") }}</el-button>
          <el-button type="primary" size="small" @click="handleEdit()">{{ $t("common.edit") }}</el-button>
          <el-button type="primary" size="small" @click="handleDel()">{{ $t("common.remove") }}</el-button>
        </div>
      </el-row>
    </el-form>
    <!-- 表格区域 -->
    <div class="tableAuto" v-if="!isCard">
      <el-table :data="tableData" style="width:100%" :height="height" @row-click="rowClick" border stripe
        :header-cell-style="{background:'#EAEBEC'}" v-loading="tableLoading" ref="refTable" highlight-current-row @sort-change='sortChange'>
        <el-table-column label="" width="50" align="center">
          <template slot-scope="scope">
            <div>
              <el-radio :label="scope.row.fid" v-model="radioId">&nbsp;</el-radio>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="index" :label="$t('common.number')" align="center" width="90">
          <template slot-scope="scope">
            {{table_index(scope.$index+1)}}
          </template>
        </el-table-column>
        <el-table-column prop="fid" width="130" show-overflow-tooltip :label="$t('basic.company')" sortable='custom'></el-table-column>
        <el-table-column prop="abbreviation" show-overflow-tooltip :label="$t('common.describe')"></el-table-column>
      </el-table>
    </div>
    <!-- 卡片区域 -->
    <div v-if="isCard">
      <div class="cardBox" style="overflow-x: hidden;" v-loading="tableLoading" :style="{height:height+'px'} ">
        <el-row :gutter="25">
          <template v-for="(item,index) in tableData">
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="4">
              <div class="card" :key="index" :class="[index == cardIndex ? 'selectCard' : '']" @click="handleIndex(index,item)">
                <p class="time" style="font-weight:bold">{{$t('basic.company')}}：{{item.fid}}</p>
                <p class="time">{{$t('common.describe')}}：{{item.abbreviation}}</p>
              </div>
            </el-col>
          </template>
          <el-empty :description="$t('common.nodata')" v-if="tableData.length == 0"></el-empty>
        </el-row>
      </div>
    </div>
    <div class="pagination-area">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[20, 30, 50, 100]"
        :page-size="pager.pageSize" :current-page="pager.pageNum" layout="total, sizes, prev, pager, next, jumper" :total="pager.total">
      </el-pagination>
    </div>

    <el-dialog v-dialogDrag :title="title" :visible.sync="isShowDialog" :close-on-click-modal="false"
      :width="screenWidth > 1024 ? '30%' : '50%'" class="isShowDialog" :before-close="handleClose">
      <el-form :model="ruleForm" label-width="85px" ref="ruleForm" size="small" :rules="rules" :hide-required-asterisk="true">
        <el-row :gutter="35">
          <el-col :xs="24">
            <el-form-item :label="$t('basic.company')" prop="fid">
              <el-input v-model="ruleForm.fid" placeholder="" :disabled="title == this.$t('dialog.edit_meter')" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item :label="$t('common.describe')" prop="abbreviation">
              <el-input v-model="ruleForm.abbreviation" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleCreateMaterial('ruleForm')"
          :loading="loading">{{ $t("common.preservation") }}</el-button>
        <el-button @click="handleClose()" size="small">{{ $t("common.cancel") }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { configure } from "@/api/configure"
export default {
  data () {
    return {
      screenWidth: document.body.clientWidth,
      height: document.body.offsetHeight - 230,
      formData: {
        keyword: "",
      },
      ruleForm: {
        fid: "",
        abbreviation: "",

      },
      rules: {
        fid: [{ required: true, message: this.$t("dialog.unit_id_not_blank"), trigger: 'blur' }],
        abbreviation: [{ required: true, message: this.$t("dialog.abbre_not_blank"), trigger: 'blur' }],
      },
      isShowDialog: false,
      icon: "icon-biaoge",
      title: this.$t("dialog.add_meter"),
      loading: false,
      tableData: [],
      tableLoading: false,
      radioId: "",
      pager: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
        field: '',
        order: '',
      },
      selectList: [{
        label: "否",
        value: "0"
      }, {
        label: "是",
        value: "1"
      }],
      cardIndex: -1,
      isCard: false,
      currObj: {}
    }
  },
  created () {
    this.getUomList()
  },
  watch: {
    screenWidth (val) {
      this.screenWidth = Number(val)
    }
  },
  mounted () {
    const that = this
    window.addEventListener('resize', function () {
      that.screenWidth = document.body.offsetWidth;
    })
  },
  methods: {
    handleSearch () {
      this.pager.pageNum = 1
      this.getUomList()
    },
    getUomList () {
      const param = {
        fid: this.formData.keyword,
        page: this.pager.pageNum,
        size: this.pager.pageSize,
        field: this.pager.field,
        order: this.pager.order,
      }
      this.tableLoading = true
      configure.getUomList(param).then(res => {
        this.tableLoading = false
        if (res.code == 200) {
          this.tableData = res.data.rows
          this.pager.total = res.data.total
        }
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange (column) {
      if (column.order === 'descending') {
        this.pager.field = column.prop
        this.pager.order = 'DESC'
      } else {
        this.pager.field = column.prop
        this.pager.order = 'ASC'
      }
      this.pager.pageNum = 1
      this.getUomList()
    },
    handleSizeChange (val) {
      this.pager.pageNum = 1
      this.pager.pageSize = val
      this.getUomList()
    },
    handleCurrentChange (val) {
      this.pager.pageNum = val;
      this.getUomList()
    },
    handleClose () {
      this.isShowDialog = false
    },
    handleAdd () {
      this.title = this.$t("dialog.add_meter")
      this.ruleForm = {}
      try {
        this.$refs['ruleForm'].resetFields();
      } catch (e) { }
      this.isShowDialog = true
    },
    handleEdit () {
      if (this.radioId === '') {
        this.$message.error($t('common.add'));
        return
      }
      this.title = this.$t("dialog.edit_meter")
      let copyRow = this.tableData.find(item => item.fid == this.radioId)
      this.ruleForm = {
        fid: copyRow.fid,
        abbreviation: copyRow.abbreviation
      }
      this.isShowDialog = true
    },
    handleDel () {
      if (this.radioId === '') {
        this.$message.error(this.$t('dialog.please_sel_del'));
        return
      }
      let that = this
      this.$customConfirm({
        content: this.$t("dialog.del_meter") + this.currObj.fid,
        onok () {
          configure.removeUom(that.radioId).then(res => {
            if (res.code == 200) {
              that.radioId = ""
              that.getUomList()
              console.info('aa-' + that.$t("common.del_succ"))
              that.$message({ duration: 1000, type: 'success', message: that.$t("common.del_succ") });
            } else {
              that.$message.error(res.msg)
            }
          })
        }
      });
    },
    handleCreateMaterial (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = Object.assign({}, this.ruleForm)
          this.loading = true
          configure.createUomInfo(param, this.title).then(res => {
            this.loading = false
            if (res.code == 200) {
              this.$refs[formName].resetFields();
              this.getUomList()
              // this.radioId = ""
              this.isShowDialog = false
            }
          }).catch(e => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    handleIndex (index, row) {
      this.cardIndex = index
      this.radioId = row.fid;
    },
    rowClick (row) {
      this.radioId = row.fid;
      this.currObj = row;
      this.$refs.refTable.setCurrentRow(row);
    },
    changePattern () {
      this.isCard = !this.isCard
      this.radioId = ''
      this.cardIndex = -1
      this.icon = this.isCard ? 'icon-qiehuan' : 'icon-biaoge'
    },
    table_index (index) {
      return (this.pager.pageNum - 1) * this.pager.pageSize + index
    },
  }
}
</script>

<style scoped>
.btngroup {
  float: right;
  margin-right: 20px;
}
.change >>> .iconfont {
  font-size: 13px !important;
}
.change {
  /* margin-top: 10px; */
}
.titmepicker >>> .el-date-editor {
  width: 100%;
}
.titmepicker >>> .el-date-editor .el-range-separator {
  width: 8%;
}
.isShowDialog >>> .el-select {
  width: 100%;
}
.tableAuto,
.cardBox {
  clear: both;
}
.pagination-area {
  padding-top: 10px;
}
.cardBox {
  overflow-y: auto;
}
.card {
  background: #eaebec;
  margin-bottom: 20px;
  padding: 10px;
  border-radius: 3px;
}
.card p {
  font-size: 12px;
  line-height: 22px;
  transform: scale(0.9);
}
.desc {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.title {
  font-weight: bold;
}
.selectCard {
  background: #f6fbff;
}
@media only screen and (min-width: 1200px) {
  .el-col-lg-5 {
    width: 19.83333%;
  }
}
.Desc {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>